Skip to content

Compile dynamic call sites with per-occurrence state - #675

Open
jogibear9988 wants to merge 3 commits into
dadhi:masterfrom
jogibear9988:fix/dynamic-call-sites-20261114
Open

jogibear9988 wants to merge 3 commits into
dadhi:masterfrom
jogibear9988:fix/dynamic-call-sites-20261114

Conversation

@jogibear9988

@jogibear9988 jogibear9988 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #665 (e95d0ffcf5bc4b33c5ea419f16d39bbb8532613c). Incremental diff over #665. The PR continues to target upstream master until the parent merges.

Compile dynamic expressions as intrinsic CallSite operations, preserving separate emitted occurrences, mutable targets, argument order, typed/void results and ref copyback. Stateful nested lambdas keep separate call sites; quoted dynamic nodes remain data.

Dynamic collection reuses #665’s structural spill tracking. The recursive visitor and its additional argument walk are removed. LightExpression lowering now builds the call-site operations directly from the original argument nodes instead of converting Light → System → Light.

Validation

Full Release solution build and Standard/LightExpression native suites pass for net8.0, net10.0 and net472. Dynamic regressions cover rebinding, reused nodes/nested lambdas, target replacement, exceptions, typed/void returns, ref aliases and property writeback. The inherited deep/wide and ordering regressions from #665 also pass.

Measurements

Fresh measurements use the #665 base e95d0ffcf5bc4b33c5ea419f16d39bbb8532613c, old #675 b06035d7cae72c827447d3c6ed166512bb51c8f6, and revised #675 9b8c52c7487503c4fa02870445942038dcbfdac0. SDK 10.0.301; Windows ARM64; task-local runtimes 8.0.28 and 10.0.9.

Both input APIs use FEC. Trees/conversions/adapters are prepared before measurement; compilation includes delegate creation. Two opposite-load-order process runs, seven rotated samples each, medians of 14 samples. Tiering disabled; one fixed logical CPU; GC.GetAllocatedBytesForCurrentThread. Calibration targets 150 ms across the three shared variants and 100 ms across the two dynamic variants. Runtime expression invocation includes uncached System Compile; dynamic runtime figures use a warmed call site and exclude first binder compilation.

The #665-base comparison isolates this feature’s additional cost; the old #675 comparison includes the traversal/lowering rework and inherited #665 changes. All slower observations are retained. Timing variation prevents a blanket speedup or no-regression claim.

In the working dynamic case, compilation allocation drops by 24 B for System-expression inputs and 504 B for LightExpression on both runtimes. Light compilation measured 4.862 → 2.722 μs on .NET 8 and 4.643 → 2.852 μs on .NET 10. Runtime allocations remain 0 B/op. Shared controls add no allocation over the #665 base.

Working dynamic case

Values are old #675 → revised #675. fixed_case is a typed dynamic call returning 7 through a constant binder. #665 alone does not support this case.

Runtime / API Compile μs Compile B/op Runtime ns Runtime B/op
net10.0 / LightExpression 4.643 → 2.852 2592.0 → 2088.0 2.379 → 2.234 0.0 → 0.0
net10.0 / System trees 3.920 → 4.001 2016.0 → 1992.0 2.140 → 2.147 0.0 → 0.0
net8.0 / LightExpression 4.862 → 2.722 2592.0 → 2088.0 2.198 → 2.135 0.0 → 0.0
net8.0 / System trees 4.088 → 4.013 2016.0 → 1992.0 2.104 → 2.114 0.0 → 0.0
Shared compilation and runtime controls

Values are #665 base → old #675 → revised #675.

Runtime / API Workload Compile μs Compile B/op Runtime ns Runtime B/op
net10.0 / LightExpression nested_lambda 2.354 → 2.404 → 2.156 2048.0 → 2048.0 → 2048.0 1.840 → 1.873 → 1.864 0.0 → 0.0 → 0.0
net10.0 / LightExpression runtime_expression_dead_switch 3.177 → 2.635 → 2.977 1672.0 → 1568.0 → 1672.0 15696.121 → 15806.436 → 15752.517 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_dead_try 3.005 → 2.646 → 2.894 1672.0 → 1568.0 → 1672.0 15618.527 → 15261.994 → 15069.481 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_deep_dead_plain 3.465 → 2.847 → 3.380 1568.0 → 1568.0 → 1568.0 15995.987 → 16351.475 → 16920.492 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_deep_dead_try 3.604 → 2.715 → 3.595 1672.0 → 1568.0 → 1672.0 16565.462 → 16458.431 → 16092.074 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_live_add 2.812 → 2.889 → 2.697 1568.0 → 1568.0 → 1568.0 15999.530 → 15938.598 → 15606.784 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_plain 2.679 → 2.662 → 2.659 1568.0 → 1568.0 → 1568.0 15262.719 → 15333.887 → 15828.328 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_wide_dead_plain 4.747 → 2.694 → 4.730 1568.0 → 1568.0 → 1568.0 15384.353 → 15980.916 → 15769.150 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression scalar_add 1.131 → 1.135 → 1.106 872.0 → 872.0 → 872.0 1.857 → 1.853 → 1.870 0.0 → 0.0 → 0.0
net10.0 / System trees nested_lambda 2.531 → 2.490 → 2.586 2048.0 → 2048.0 → 2048.0 1.840 → 1.884 → 1.866 0.0 → 0.0 → 0.0
net10.0 / System trees runtime_expression_dead_switch 3.980 → 3.630 → 4.000 1672.0 → 1568.0 → 1672.0 15981.135 → 15859.134 → 15760.582 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_dead_try 3.939 → 3.406 → 3.832 1672.0 → 1568.0 → 1672.0 15617.590 → 15695.229 → 15722.969 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_deep_dead_plain 4.217 → 3.690 → 4.198 1568.0 → 1568.0 → 1568.0 15934.435 → 16517.686 → 15886.440 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_deep_dead_try 4.302 → 3.557 → 4.506 1672.0 → 1568.0 → 1672.0 16440.662 → 16803.629 → 16156.652 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_live_add 3.748 → 3.614 → 3.624 1568.0 → 1568.0 → 1568.0 15285.482 → 15437.597 → 15609.233 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_plain 3.421 → 3.390 → 3.433 1568.0 → 1568.0 → 1568.0 15526.491 → 15717.176 → 15567.980 4248.0 → 4248.0 → 4248.0
net10.0 / System trees runtime_expression_wide_dead_plain 9.813 → 3.685 → 9.763 1568.0 → 1568.0 → 1568.0 15078.139 → 15163.956 → 15161.766 4256.0 → 4256.0 → 4256.0
net10.0 / System trees scalar_add 1.446 → 1.452 → 1.476 872.0 → 872.0 → 872.0 1.900 → 1.894 → 1.941 0.0 → 0.0 → 0.0
net8.0 / LightExpression nested_lambda 3.550 → 3.821 → 3.351 2048.0 → 2048.0 → 2048.0 2.595 → 2.689 → 2.989 0.0 → 0.0 → 0.0
net8.0 / LightExpression runtime_expression_dead_switch 4.443 → 4.185 → 3.857 1672.0 → 1568.0 → 1672.0 18770.025 → 26023.776 → 22949.573 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_dead_try 5.745 → 4.777 → 6.665 1672.0 → 1568.0 → 1672.0 18334.222 → 26015.350 → 18298.762 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_deep_dead_plain 7.098 → 4.642 → 4.792 1568.0 → 1568.0 → 1568.0 28496.849 → 29676.236 → 30619.671 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_deep_dead_try 5.540 → 3.505 → 4.653 1672.0 → 1568.0 → 1672.0 18116.636 → 22758.356 → 22406.144 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_live_add 4.660 → 3.892 → 4.164 1568.0 → 1568.0 → 1568.0 41353.094 → 42741.845 → 33082.685 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_plain 3.314 → 3.347 → 3.286 1568.0 → 1568.0 → 1568.0 20705.080 → 19325.709 → 22898.582 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression runtime_expression_wide_dead_plain 8.364 → 3.813 → 6.697 1568.0 → 1568.0 → 1568.0 17452.968 → 17484.720 → 20122.543 4256.1 → 4256.1 → 4256.1
net8.0 / LightExpression scalar_add 1.397 → 1.820 → 1.618 872.0 → 872.0 → 872.0 2.541 → 2.495 → 2.555 0.0 → 0.0 → 0.0
net8.0 / System trees nested_lambda 4.597 → 5.626 → 4.890 2048.0 → 2048.0 → 2048.0 3.037 → 3.174 → 2.891 0.0 → 0.0 → 0.0
net8.0 / System trees runtime_expression_dead_switch 4.457 → 4.889 → 7.049 1672.0 → 1568.0 → 1672.0 21108.353 → 18288.489 → 17669.050 4256.1 → 4256.1 → 4256.1
net8.0 / System trees runtime_expression_dead_try 6.274 → 6.420 → 6.357 1672.0 → 1568.0 → 1672.0 23988.670 → 27508.056 → 28455.012 4252.1 → 4252.1 → 4252.1
net8.0 / System trees runtime_expression_deep_dead_plain 6.721 → 5.821 → 5.562 1568.0 → 1568.0 → 1568.0 16342.976 → 16873.882 → 15564.009 4256.1 → 4256.1 → 4256.1
net8.0 / System trees runtime_expression_deep_dead_try 4.770 → 3.832 → 5.434 1672.0 → 1568.0 → 1672.0 15386.043 → 14988.348 → 13808.167 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_live_add 4.919 → 4.879 → 5.166 1568.0 → 1568.0 → 1568.0 14812.159 → 14570.935 → 15223.537 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_plain 4.850 → 4.952 → 5.428 1568.0 → 1568.0 → 1568.0 19787.620 → 21910.230 → 19151.382 4248.1 → 4248.1 → 4248.1
net8.0 / System trees runtime_expression_wide_dead_plain 11.468 → 4.646 → 12.955 1568.0 → 1568.0 → 1568.0 17564.822 → 21979.318 → 24009.136 4256.1 → 4256.1 → 4256.1
net8.0 / System trees scalar_add 1.941 → 2.141 → 2.478 872.0 → 872.0 → 872.0 3.015 → 2.883 → 3.382 0.0 → 0.0 → 0.0

Emitted IL for shared controls: identical between the compared revisions.

Emitted IL for working dynamic case: identical between the compared revisions.

Runtime expression invocation must follow System’s structural stack-spill ordering even when FEC prunes a dead try or goto branch. Inspect argument syntax without reducing opaque extension nodes, and defer the implicit Compile call where required.

Tests compare result, exception and side-effect order for valid, null and invalid receivers, including dead conditional branches.
Record structural spill information during normal argument collection and inspect only discarded conditional/switch syntax with an iterative cursor stack. Preserve lambda/quote boundaries and keep emitted region metadata separate.

Use child cursors and an inline buffer to avoid wide sibling queues, advance unary chains in place, and bypass the cursor frame for simple discarded nodes. Keep the original ordering regressions and add deep/wide syntax, initializer, boundary and extension coverage.

Validation: full Release build; Standard and LightExpression suites on net8.0, net10.0 and net472; fresh compile/runtime/allocation comparisons against master and the previous PR head. Shared delegates retain identical emitted IL.
Lower dynamic nodes to CallSite operations, preserving per-occurrence state, mutable targets, evaluation order and ref copyback. Preserve quoted nodes as data and avoid deduplicating stateful nested lambdas.

Reuse structural spill tracking from the parent change instead of a recursive argument visitor. Build call-site operations directly in either expression API, preserving original LightExpression arguments without a System conversion round-trip.

Validation: full Release build and Standard/LightExpression suites on net8.0, net10.0 and net472. Fresh compile/runtime comparisons include the parent, previous feature head and working dynamic case. Measured dynamic compilation saves 24 B for System-expression inputs and 504 B for LightExpression; runtime remains allocation-free in that case.
@jogibear9988
jogibear9988 force-pushed the fix/dynamic-call-sites-20261114 branch from b06035d to 9b8c52c Compare November 15, 2026 09:23
@jogibear9988

Copy link
Copy Markdown
Contributor Author

This PR had the same recursive visitor / repeated argument walk discussed in #665. It is now rebuilt on #665 (parent e95d0ff), with the dynamic feature in 9b8c52c. The branch was updated with an explicit force-with-lease check against the previous head.

Dynamic target ordering now reuses the collector's structural spill tracking and iterative inspection of discarded syntax. The visitor is removed. LightExpression call-site lowering also uses the original argument nodes directly, eliminating the Light → System → Light round-trip. Existing occurrence/state, target mutation, exception and ref-copyback tests remain.

Full Release build and Standard/LightExpression suites pass on .NET 8, .NET 10 and net472, including the inherited deep/wide regressions. Fresh measurements of the working dynamic case show:

API Compile allocation, old → new .NET 8 compile μs, old → new .NET 10 compile μs, old → new
System-expression inputs 2016 → 1992 B 4.088 → 4.013 3.920 → 4.001
LightExpression 2592 → 2088 B 4.862 → 2.722 4.643 → 2.852

Runtime remains 0 B/op; emitted IL for this measured dynamic case is unchanged. Shared controls add no allocation over #665. All runtime timings, slower observations, and the comparison with the #665 base are in the updated description.

The PR stays in dadhi's repo and targets master; its description links #665 as a dependency and provides the incremental diff, so the parent should merge first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant